{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "eea166d0",
   "metadata": {},
   "source": [
    "**2022/10/24 礼仪的week10**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c31da61b",
   "metadata": {},
   "source": [
    "# 本周学习知识点\n",
    "> **1.while循环**\n",
    ">\n",
    "> **2.购物车实践**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bebf665f",
   "metadata": {},
   "source": [
    "# 1.while 循环（当什么成立了就怎么怎么）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74f8b926",
   "metadata": {},
   "source": [
    "* 回顾：for 循环：for+range()可以指定循环的次数——>关于网页URL队列数据的抓取\n",
    "* while循环=当某种条件成立时，停止循环\n",
    "* while True:无限循环/死循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8ce58c3c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿，我是沙 0\n",
      "你是风儿，我是沙 1\n",
      "你是风儿，我是沙 2\n",
      "你是风儿，我是沙 3\n",
      "你是风儿，我是沙 4\n"
     ]
    }
   ],
   "source": [
    "#1.指定次数\n",
    "count=0\n",
    "while True:\n",
    "    print(\"你是风儿，我是沙\",count)\n",
    "    count+=1\n",
    "    if count==5:\n",
    "        break#终止循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1e26df9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：admin\n",
      "密码：123456\n",
      "登陆成功~欢迎admin\n"
     ]
    }
   ],
   "source": [
    "# 2.while True 循环，可以让用户重复输入\n",
    "user01 = \"admin\"\n",
    "password01 = \"123456\"\n",
    "count=0 #记数器\n",
    "while True:\n",
    "    username = input(\"账号：\")\n",
    "    password = input(\"密码：\")  \n",
    "    if user01 == username and  password01 == password:\n",
    "        print(\"登陆成功~欢迎\"+user01)\n",
    "        break # 中止循环\n",
    "    else:\n",
    "        if count<2:\n",
    "            print(\"账号密码不对，请重新输入！您还有\"+str(2-count)+\"次尝试机会\")\n",
    "        else:\n",
    "            print(\"已锁定，请一个小时后再尝试\")\n",
    "            break\n",
    "        count=count+1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6680fafd",
   "metadata": {},
   "source": [
    "# 购物车项目"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "242ad64a",
   "metadata": {},
   "source": [
    "* 1.运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "* 2.让用户输入商品编号进行商品的购买。\n",
    "* 3.用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "* 4.用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "24a3f6d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入支付宝余额：2\n",
      "---------商品列表----------\n",
      "蜜雪柠檬水 6元\n",
      "蜜桃四季春 10元\n"
     ]
    }
   ],
   "source": [
    "#运行程序后，让用户输入支付宝余额✔，然后打印我们的商品列表给用户\n",
    "product_list=[\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄啵啵奶茶\",8]\n",
    "]\n",
    "\n",
    "Alipay=input(\"请输入支付宝余额：\")\n",
    "print(\"---------商品列表----------\")\n",
    "print(product_list[0][0],str(product_list[0][1])+\"元\")#格式化输出\n",
    "print(product_list[1][0],str(product_list[1][1])+\"元\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "497d3f71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5c1f100d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入支付宝余额：2\n",
      "---------商品列表----------\n",
      "蜜雪柠檬水 6元\n",
      "蜜桃四季春 10元\n",
      "芒果奶盖 12元\n",
      "葡萄啵啵奶茶 8元\n"
     ]
    }
   ],
   "source": [
    "#运行程序后，让用户输入支付宝余额✔，然后打印我们的商品列表给用户\n",
    "product_list=[\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄啵啵奶茶\",8]\n",
    "]\n",
    "\n",
    "Alipay=input(\"请输入支付宝余额：\")\n",
    "print(\"---------商品列表----------\")\n",
    "for i in range(4):\n",
    "    print(product_list[i][0],str(product_list[i][1])+\"元\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "35bf5807",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入支付宝余额：2\n",
      "---------商品列表----------\n",
      "编号: (0): 蜜雪柠檬水 6元\n",
      "编号: (1): 蜜桃四季春 10元\n",
      "编号: (2): 芒果奶盖 12元\n",
      "编号: (3): 葡萄啵啵奶茶 8元\n",
      "请输入商品编号：1\n",
      "钱不够，别买\n",
      "---------商品列表----------\n",
      "编号: (0): 蜜雪柠檬水 6元\n",
      "编号: (1): 蜜桃四季春 10元\n",
      "编号: (2): 芒果奶盖 12元\n",
      "编号: (3): 葡萄啵啵奶茶 8元\n",
      "请输入商品编号：1\n",
      "钱不够，别买\n",
      "---------商品列表----------\n",
      "编号: (0): 蜜雪柠檬水 6元\n",
      "编号: (1): 蜜桃四季春 10元\n",
      "编号: (2): 芒果奶盖 12元\n",
      "编号: (3): 葡萄啵啵奶茶 8元\n",
      "请输入商品编号：1\n",
      "钱不够，别买\n",
      "---------商品列表----------\n",
      "编号: (0): 蜜雪柠檬水 6元\n",
      "编号: (1): 蜜桃四季春 10元\n",
      "编号: (2): 芒果奶盖 12元\n",
      "编号: (3): 葡萄啵啵奶茶 8元\n",
      "请输入商品编号：1\n",
      "钱不够，别买\n",
      "---------商品列表----------\n",
      "编号: (0): 蜜雪柠檬水 6元\n",
      "编号: (1): 蜜桃四季春 10元\n",
      "编号: (2): 芒果奶盖 12元\n",
      "编号: (3): 葡萄啵啵奶茶 8元\n",
      "请输入商品编号：1\n",
      "钱不够，别买\n",
      "---------商品列表----------\n",
      "编号: (0): 蜜雪柠檬水 6元\n",
      "编号: (1): 蜜桃四季春 10元\n",
      "编号: (2): 芒果奶盖 12元\n",
      "编号: (3): 葡萄啵啵奶茶 8元\n",
      "请输入商品编号：1\n",
      "钱不够，别买\n",
      "---------商品列表----------\n",
      "编号: (0): 蜜雪柠檬水 6元\n",
      "编号: (1): 蜜桃四季春 10元\n",
      "编号: (2): 芒果奶盖 12元\n",
      "编号: (3): 葡萄啵啵奶茶 8元\n",
      "请输入商品编号：1\n",
      "钱不够，别买\n",
      "---------商品列表----------\n",
      "编号: (0): 蜜雪柠檬水 6元\n",
      "编号: (1): 蜜桃四季春 10元\n",
      "编号: (2): 芒果奶盖 12元\n",
      "编号: (3): 葡萄啵啵奶茶 8元\n",
      "请输入商品编号：1\n",
      "钱不够，别买\n",
      "---------商品列表----------\n",
      "编号: (0): 蜜雪柠檬水 6元\n",
      "编号: (1): 蜜桃四季春 10元\n",
      "编号: (2): 芒果奶盖 12元\n",
      "编号: (3): 葡萄啵啵奶茶 8元\n",
      "请输入商品编号：q\n",
      "您已经购买了[]\n",
      "您的余额还有2,欢迎下次光临\n"
     ]
    }
   ],
   "source": [
    "#运行程序后，让用户输入支付宝余额✔，然后打印我们的商品列表给用户\n",
    "product_list=[\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄啵啵奶茶\",8]\n",
    "]\n",
    "shopping_car=[]\n",
    "Alipay=int(input(\"请输入支付宝余额：\"))\n",
    "while True:\n",
    "    print(\"---------商品列表----------\")\n",
    "    for i in range(4):\n",
    "        print(\"编号:\",\"(\"+str(i)+\"):\",product_list[i][0],str(product_list[i][1])+\"元\")#格式化输出\n",
    "        #2.让用户输入商品编号进行购买——直接用商品列表的index 作为商品编号\n",
    "    user_choice=input(\"请输入商品编号：\")\n",
    "    # 情景1：正在购买，输入商品编号\n",
    "    \n",
    "    if user_choice.isdigit():\n",
    "        if Alipay >=product_list[int(user_choice)][1]:\n",
    "            Alipay=Alipay-product_list[int(user_choice)][1]\n",
    "            shopping_car.append(product_list[int(user_choice)][1])\n",
    "            print(\"买好啦！账户还剩下%s元\"%(Alipay))\n",
    "        else:\n",
    "            print(\"钱不够，别买\")\n",
    "# 情景2：正在购买，输入商品编号\n",
    "    elif user_choice ==\"q\":\n",
    "        print(\"您已经购买了\"+str(shopping_car))\n",
    "        print(\"您的余额还有%s,欢迎下次光临\"%(Alipay))\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ace8812b",
   "metadata": {},
   "source": [
    "* 期中测试：\n",
    "列表，字符串，条件判断，字典\n",
    "第二章项目练习要注意看"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02bbdb39",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
